<?php

namespace app\backend\controller\hrm;

use app\common\controller\Basic;
use app\common\service\hrm\CompanyService;

/**
 * 公司单位
 * @author kaige
 * @date 2022-04-28
 */
class Company extends Basic
{
  // 初始化
  protected function initialize()
  {
    parent::initialize();
    // 当前控制器服务实例化
    $this->service = new CompanyService();
  }

  /**
   * 列表
   */
  public function index()
  {
    if ($this->request->isGet()) {
      $data = input('param.');
      return json($this->service->_list($data, false));
    }
    return _jerr(1000);
  }

  /**
   * 添加
   */
  public function add()
  {
    if ($this->request->isPost()) {
      $data = input('param.');
      $this->_form_filter($data);
      return json($this->service->saveForm($data));
    }
    if ($this->request->isGet()) {
      return _jok();
    }
    return _jerr(1000);
  }

  /**
   * 编辑
   */
  public function edit()
  {
    if ($this->request->isPost()) {
      $data = input('param.');
      $this->_form_filter($data);
      return json($this->service->saveForm($data));
    }
    if ($this->request->isGet()) {
      $id = input('id/d', 0);
      $data = $this->service->getInfoById($id);
      if (isset($data['logo'])) {
        $data['logo_url'] = _img($data['logo']);
      }
      if (isset($data['bl_img'])) {
        $data['bl_img_url'] = _img($data['bl_img']);
      }
      if (isset($data['bl_sfz_img'])) {
        $data['bl_sfz_img_url'] = _img($data['bl_sfz_img']);
      }
      // if(isset($data['bl_xkz_img'])){
      //   $data['bl_xkz_img_url'] = _img($data['bl_xkz_img']);
      // }
      return _jok('ok', $data);
    }
    return _jerr(1000);
  }

  /**
   * 添加编辑-表单数据处理
   * @param array $data
   */
  protected function _form_filter(&$data)
  {
    if ($this->request->isPost()) {
      $data['status'] = checkon('status', $data);
      if (isset($data['id']) && isset($data['pid'])) {
        if ($data['id'] == $data['pid']) {
          $data['pid'] = 0;
        }
      }
    }
  }

  /**
   * 删除
   */
  public function del()
  {
    if ($this->request->isPost()) {
      $id = input('id/d', 0);
      if ($this->service->delete($id)) {
        return _jok(1025);
      };
      return _jerr(1026);
    }
    return _jerr(1000);
  }

  /**
   * 启/禁用
   */
  public function setdisabled()
  {
    if ($this->request->isPost()) {
      $id = input('id/d', 0);
      $field = input('field', '');
      $value = input('value/d', 0);
      if ($this->service->updateField($id, $field, $value)) {
        return _jok(1031);
      };
      return _jerr(1032);
    }
    return _jerr(1000);
  }
}
